<template>
  <el-card shadow="never" class="mb10">
    <el-row>
      <el-form :inline="true" :model="queryData" ref="formQueryRef">
        <el-form-item label="手机号码" prop="mobile">
          <el-input
            placeholder="请输入手机号码"
            v-model.number="queryData.mobile"
            clearable
          />
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
          <el-input
            placeholder="请输入姓名"
            v-model.trim="queryData.nickname"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            class="ml20"
            v-model="queryData.status"
            clearable
            placeholder="请选择状态"
            @change="initData"
          >
            <el-option label="锁定" :value="1"> </el-option>
            <el-option label="正常" :value="0"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="flex-right">
        <el-button type="primary" :icon="RefreshLeft" @click="resetForm">
          重置
        </el-button>
        <el-button
          type="primary"
          :icon="Search"
          class="ml20"
          @click="initData"
          v-enter="initData"
        >
          搜索
        </el-button>
      </div>
    </el-row>
  </el-card>
  <el-card shadow="never">
    <div class="mb10">
      <el-button type="primary" @click="onDialogStaff()">+增加</el-button>
      <!-- <el-button type="danger" @click="onDelete">删除</el-button> -->
    </div>
    <el-table border stripe :data="tableData" v-loading="loading">
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column
        v-for="item in options"
        :key="item.type"
        :prop="item.props"
        :type="item.type"
        :label="item.label"
        :width="item.width"
        :align="item.align"
        show-overflow-tooltip
        :fixed="item.fixed"
      >
        <template v-slot:default="{ row }" v-if="item.props === 'createTime'">
          {{ dayjs(row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
        <template v-slot:default="{ row }" v-if="item.props === 'status_desc'">
          <el-text :type="row.status ? 'danger' : 'success'">
            {{ row.status === 1 ? "锁定" : "正常" }}
          </el-text>
        </template>
        <template v-slot:default="{ row }" v-if="item.props === 'actions'">
          <el-button
            type="primary"
            text
            size="small"
            @click="onDialogStaff(row)"
          >
            编辑
          </el-button>
          <el-button type="warning" text size="small" @click="onLock(row)">
            {{ row.status ? "解锁" : "锁定" }}
          </el-button>
          <el-button type="danger" text size="small" @click="onFrozen(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="mt-4"
      :total="total"
      :page-size="queryData.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-card>

  <DialogStaff
    ref="dialogStaffRef"
    @success="handleCurrentChange(1)"
  ></DialogStaff>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Search, RefreshLeft } from "@element-plus/icons-vue";
import { dayjs } from "element-plus";
import api from "@/api/staff.js";
import { options } from "./options.js";
import DialogStaff from "./components/dialogStaff.vue";

const queryData = ref({
  pageNo: 1,
  pageSize: 10,
});
const dialogStaffRef = ref();
const formQueryRef = ref();
const loading = ref(false);

const tableData = ref([]);
const total = ref(0);

const initData = async () => {
  loading.value = true;
  try {
    const res = await api.page(queryData.value);
    tableData.value = res.data?.list || [];
    total.value = res.data?.total;
  } finally {
    loading.value = false;
  }
};

const handleCurrentChange = (val) => {
  queryData.value.pageNo = val;
  initData(queryData.value);
};
const handleSizeChange = (val) => {
  queryData.value.pageSize = val;
  initData(queryData.value);
};
const resetForm = () => {
  formQueryRef.value.resetFields();
};

const onDialogStaff = (row = null) => {
  dialogStaffRef.value.openDialog(row);
};

const onLock = (row) => {
  ElMessageBox.confirm(
    `确认是否<span style='color:red;'>${
      row.status ? "解锁" : "锁定"
    }</span>成员<span style='color:red;'>${row.username}</span>`,
    "复核提示",
    {
      type: "warning",
      dangerouslyUseHTMLString: true,
      confirmButtonText: "确定",
      cancelButtonText: "取消",
    }
  )
    .then(async () => {
      const res = await api.updateStatus({
        id: row.id,
        status: row.status ? 0 : 1,
      });
      if (res.data) {
        ElMessage.success(res.msg || `${row.status ? "解锁" : "锁定"}成功！`);
        handleCurrentChange(1);
      } else {
        ElMessage.error(res.msg || `${row.status ? "解锁" : "锁定"}失败！`);
      }
    })
    .catch(() => {});
};
const onFrozen = (row) => {
  ElMessageBox.confirm(
    `确认是否<span style='color:red;'>删除</span>成员<span style='color:red;'>${row.username}</span>`,
    "删除提示",
    {
      type: "warning",
      dangerouslyUseHTMLString: true,
      confirmButtonText: "确定",
      cancelButtonText: "取消",
    }
  )
    .then(async () => {
      const res = await api.delete({
        id: row.id,
      });
      if (res.data) {
        ElMessage.success(res.msg || `删除成功！`);
        handleCurrentChange(1);
      } else {
        ElMessage.error(res.msg || `删除失败！`);
      }
    })
    .catch(() => {});
};

onMounted(() => {
  initData();
});
</script>
<style lang="scss" scoped>
.henader-card {
  margin-bottom: 20px;
}
.mt-4 {
  float: right;
  text-align: right;
  margin-top: 20px;
  margin-bottom: 20px;
}
.el-select {
  width: 180px;
}
.el-input {
  width: 180px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
